package com.lagou.edu.controller;

import com.lagou.edu.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName UserController
 * @Description TODO
 * @Auth ying.xiao
 * @Date 2021/4/20
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceImpl userServiceImpl;

    @GetMapping("/register/{email}/{password}/{code}")
    public boolean register(HttpServletResponse response,@PathVariable String email,@PathVariable String password,@PathVariable String code){
        String register = userServiceImpl.register(email, password, code);
        //登录成功设置token令牌
        if(null != register){
            Cookie cookie = new Cookie("token", register);
            cookie.setPath("/");
            response.addCookie(cookie);
            return true;
        }
        return  false;
    }
    @GetMapping("/isRegistered/{email}")
    public boolean isRegister(@PathVariable String email){
        return userServiceImpl.isRegister(email);
    }
    @GetMapping("/info/{token}")
    public String info(@PathVariable String token){
        return userServiceImpl.info(token);
    }
    @GetMapping("/login/{email}/{password}")
    public String login(HttpServletResponse response, @PathVariable String email, @PathVariable String password){
        String token =  userServiceImpl.login(email,password);
        //登录成功设置token令牌
        if(null != token){
            Cookie cookie = new Cookie("token", token);
            cookie.setPath("/");
            response.addCookie(cookie);
            return email;
        }
        return null;
    }
}
